﻿////////////////////////////////////////////////////////////
//                                                        //
//                 Internet Shop Team                     //
//                                                        //
////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace InternetShop.Code
{
    [Serializable]
    public class Product
    {
        #region Properties

        public long? ID { get; set; }

        public string Brand { get; set; }

        public string Model { get; set; }

        public decimal? Price { get; set; }

        public string Description { get; set; }

        public long? Quantity { get; set; }

        public string ImagePath { get; set; }

        #endregion

        #region Methods

        public static IEnumerable<Product> GetProducts()
        {
            List<Product> result = new List<Product>();

            DataHelper.ExecFillingProc("ProductGetAll",
                                delegate(IDataReader r)
                                {
                                    while (r.Read())
                                    {
                                        Product tmp = new Product();
                                        tmp.ID = (long?)r["ID"];
                                        tmp.Brand = r["Brand"].ToString();
                                        tmp.Model = r["Model"].ToString();
                                        tmp.Price = (decimal?)r["Price"];
                                        tmp.Description = r["Description"].ToString();
                                        tmp.Quantity = (long?)r["Quantity"];
                                        tmp.ImagePath = r["ImagePath"].ToString();
                                        result.Add(tmp);
                                    }
                                });
            
            return result;
        }
        /**
         * <summary>Вытягивает из базы продукт по его айди</summary>
         * */
        public static Product LoadById(long id)
        {
            Product tmp = new Product();
            SqlParameter[] dbParams = {new SqlParameter("@ProdId", id)};
            DataHelper.ExecFillingProc("ProductLoadById", dbParams, 
                                        delegate(IDataReader r)
                                        {
                                            while (r.Read())
                                            {
                                                tmp.ID = (long?)r["ID"];
                                                tmp.Brand = r["Brand"].ToString();
                                                tmp.Model = r["Model"].ToString();
                                                tmp.Price = (decimal?)r["Price"];
                                                tmp.Description = r["Description"].ToString();
                                                tmp.Quantity = (long?)r["Quantity"];
                                                tmp.ImagePath = r["ImagePath"].ToString();
                                            }
                                        });
            return tmp;
        }

        #endregion
    }
}